package com.ewanghuiju.app.ui.my.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.my.FeedbackContract;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.presenter.my.FeedbackPresenter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 意见反馈
 * @time 2020/2/19
 */
public class FeedbackActivity extends BaseActivity<FeedbackPresenter> implements FeedbackContract.View {

    @BindView(R.id.view_line_one)
    View viewLineOne;
    @BindView(R.id.view_line_two)
    View viewLineTwo;
    @BindView(R.id.et_feed_back_text)
    EditText etFeedBackText;
    @BindView(R.id.tv_text_num)
    TextView tvTextNum;

    @Override
    protected int getLayout() {
        return R.layout.activity_feed_back;
    }

    @Override
    protected void initEventAndData() {
        GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[]{Color.parseColor("#DA291C"), Color.parseColor("#FFFFFF")});
        viewLineOne.setBackground(gradientDrawable);
        viewLineTwo.setBackground(gradientDrawable);

        etFeedBackText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvTextNum.setText(s.length() + "");
            }
        });
    }

    @Override
    protected int getTitleIntText() {
        return R.string.feed_back_title;
    }

    @OnClick({R.id.tv_commit})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {
            //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_commit:
                String etStr = etFeedBackText.getText().toString();
                if (TextUtils.isEmpty(etStr)) {
                    StyleableToast.makeText(mContext, "请输入您的问题或建议", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }

                String platform_type = "";
                String version = "";
                try {
                    PackageManager pm = mContext.getPackageManager();
                    PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
                    version = pi.versionName;
                    platform_type = android.os.Build.MODEL;
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
                LoadingDialogUtils.show(mContext);
                mPresenter.ptiopinion(etStr, version, platform_type);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showContent(BaseResponse bean) {
        //获取数据成功
        StyleableToast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        onBackPressedSupport();
    }

    @Override
    public void showContentError() {

    }
}
